var express = require('express');
var router = express.Router();
var stuModel = require('../database/models/stuModels')

router.post('/',(req,res)=>{
  console.log(req.body); //得到前端提交的数据包
  //把数据包放到数据库，stu表里面
  let {username,score} = req.body
  new stuModel({ //新增一条数据对象
    'username':username,
    'score':score
  }).save().then(results=>{ //执行保存
    console.log(results)
    res.send({
      code:1,
      msg:'新增成功'
    })
  })
})

router.get('/',(req,res)=>{ //加载学员列表
  // console.log(req.headers);
  stuModel.find().then(results=>{
    // console.log(results);
    res.send({
      code:1,
      msg:'查询学员成功',
      data:results
    })
  })
})

router.delete('/',(req,res)=>{ //删除学员
  console.log(req.query);
  let {_id} = req.query
  stuModel.remove({_id}).then(results=>{
    res.send({
      code:1,
      msg:'删除成功'
    })
  })
})

router.put('/',(req,res)=>{ //更新学员
  console.log(req.body);
  let {_id,username,score} = req.body
  stuModel.updateOne({_id},{username,score}).then(results=>{
    res.send({
      code:1,
      msg:'更新成功'
    })
  })
})

module.exports = router;
